package io.renren.modules.matlist.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;

import java.util.*;
//import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.modules.matlist.controller.TrustAllHttpsCertificates;
import io.renren.modules.matlist.dao.MatlistDao;
import io.renren.modules.matlist.entity.MatlistEntity;
import io.renren.modules.matlist.entity.PalletmatEntity;
import io.renren.modules.matlist.service.MatlistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
@Slf4j
@Service("matlistService")
public class MatlistServiceImpl extends ServiceImpl<MatlistDao, MatlistEntity> implements MatlistService {
    @Autowired
    MatlistDao matlistDao;

    @Override
    public List<MatlistEntity> listMat(String pid) {
        return matlistDao.listMat(pid);
    }

    @Override
    public List<MatlistEntity> listPileMat(String pid) {
        System.out.println(matlistDao.listPileMat(pid));
        return matlistDao.listPileMat(pid);
    }

    @Override
    public String listPallet(String palletCode) {
//        RestTemplate restTemplate = new RestTemplate();
        RestTemplate restTemplate;
        try {
            restTemplate = TrustAllHttpsCertificates.createRestTemplate();
        } catch (Exception e) {
            throw new RuntimeException("Failed to create rest template", e);
        }

        Map<String, Object> requestBody = new HashMap<>();
        Map<String, String> inputs = new HashMap<>();
//        inputs.put("PalletCode", palletCode);
        inputs.put("Container", palletCode);
        requestBody.put("Inputs", inputs);

        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody);

        ResponseEntity<String> response = restTemplate.exchange(
                "https://MesWebCluster.cmhijs.cn/Apriso/httpServices/operations/IF_MES_UWB_GetContainerInfo",
                HttpMethod.POST,
                entity,
                String.class
        );
        System.out.println("======这是返回数据========");
        System.out.println(response.getBody());
        System.out.println("======这是返回数据========");



//以下是返回值为String时
        try {
            // Parse the JSON response
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> jsonMap = mapper.readValue(response.getBody(), new TypeReference<Map<String, Object>>(){});
            Map<String, Object> outputs = (Map<String, Object>) jsonMap.get("Outputs");
            Map<String, Object> containerInfo = (Map<String, Object>) outputs.get("ContainerInfo");
            List<Map<String, Object>> parts = (List<Map<String, Object>>) containerInfo.get("parts");

            // Convert the parts object to JSON and return it
            String partsJson = mapper.writeValueAsString(parts);
            System.out.println("======这是托盘数据========");
            log.info(partsJson);
            System.out.println(partsJson);
            System.out.println("======这是托盘数据========");
            return partsJson;
        } catch (Exception e) {
            // Handle the exception. This could involve logging the error, notifying the user, etc.
            e.printStackTrace();
            return null;
        }
    }

}